@echo off

:: 指定端口
set PORT=8000

:: 检查端口是否被占用
for /f "tokens=5" %%a in ('netstat -aon ^| findstr :%PORT%') do (
    echo Port %PORT% is in use. Attempting to close it...
    taskkill /PID %%a /F
)

:: 运行 npm run dev 并在后台启动
echo Running npm run dev...
start /B cmd /c npm run dev -- --port %PORT%

:: 等待几秒钟，让服务器有时间启动
timeout /t 15 /nobreak

:: 打开默认浏览器访问 localhost:8000
echo Opening browser...
start http://localhost:%PORT%

:: 保持窗口打开
echo.
echo Project is running on port %PORT%. Press Ctrl+C to stop the server.
echo Press any key to close this window...
pause >nul